<template>
  <div class="artist-detail-container">
    <div class="back-button">
      <el-button icon="el-icon-arrow-left" type="text" size="medium" @click="goBack">
        <span style="margin-left: 5px; font-size: 16px;">返回列表</span>
      </el-button>
    </div>
    <div class="artist-header" v-if="artist">
      <div class="artist-info">
        <div class="artist-avatar">
          <img :src="artist.avatar || require('@/assets/history/opera_mlf.jpg')" :alt="artist.artistName">
        </div>
        <div class="artist-basic-info">
          <h1>{{ artist.artistName }}</h1>
          <div class="artist-tags">
            <template v-if="artist.categoryId">
              <el-tag size="medium" type="danger" v-for="(catId, index) in artist.categoryId.split(',')" :key="'cat-'+index" class="category-tag">{{ getCategoryName(catId) }}</el-tag>
            </template>
            <el-tag size="medium" type="warning" v-if="artist.school" class="category-tag">{{ artist.school }}</el-tag>
          </div>
          <p class="artist-bio">{{ artist.description || '暂无艺术家介绍' }}</p>
          <div class="artist-meta">
            <div class="meta-item">
              <i class="el-icon-date"></i>
              <span>生卒年: {{ artist.birthTime ? artist.birthTime : '不详' }} - {{ artist.deathTime ? artist.deathTime : '至今' }}</span>
            </div>
          </div>
        </div>
      </div>
    </div>
    
    <div class="content-tabs">
      <el-tabs v-model="activeTab" type="border-card">
        <el-tab-pane label="艺术特点" name="features">
          <div class="tab-content features-content" v-if="artist">
            <div class="feature-cards">
              <div class="feature-card" v-for="(feature, index) in artist.performanceFeatures" :key="index">
                <div class="feature-icon"><i :class="feature.icon"></i></div>
                <div class="feature-text">
                  <h3>{{ feature.title }}</h3>
                  <p>{{ feature.description }}</p>
                </div>
              </div>
            </div>
          </div>
        </el-tab-pane>
        
        
        <el-tab-pane label="代表作品" name="works">
          <div class="tab-content works-content" v-if="artist && artist.works && artist.works.length > 0">
            <div class="works-grid">
              <el-card v-for="(work, index) in artist.works" :key="index" class="work-card" shadow="hover">
                <div class="work-image">
                  <img :src="work.image" :alt="work.title">
                </div>
                <div class="work-info">
                  <h3>{{ work.title }}</h3>
                  <p>{{ work.description }}</p>
                  <div class="work-meta">
                    <span>角色: {{ work.role }}</span>
                  </div>
                </div>
              </el-card>
            </div>
          </div>
          
          <div class="no-works-data" v-else>
            <img src="@/assets/opera_logo.png" alt="暂无数据">
            <p>暂无代表作品信息</p>
          </div>
        </el-tab-pane>
        
        <el-tab-pane label="历史相册" name="gallery">
          <div class="tab-content gallery-content" v-if="artist && artist.photos && artist.photos.length > 0">
            <el-row :gutter="20">
              <el-col :xs="24" :sm="12" :md="8" :lg="6" v-for="(photo, index) in artist.photos" :key="index" class="gallery-item">
                <div class="photo-card" @click="viewLargeImage(photo.url, photo.caption)">
                  <img :src="photo.url || require('@/assets/history/opera_mlf.jpg')" :alt="photo.caption">
                  <div class="photo-caption">{{ photo.caption }}</div>
                </div>
              </el-col>
            </el-row>
          </div>
          
          <div class="no-gallery-data" v-else>
            <img src="@/assets/opera_logo.png" alt="暂无数据">
            <p>暂无历史相册信息</p>
          </div>
        </el-tab-pane>
      </el-tabs>
    </div>
    
    <!-- 放大查看图片的对话框 -->
    <el-dialog
      :visible.sync="dialogVisible"
      :title="dialogImageTitle"
      width="70%"
      center>
      <img width="100%" :src="dialogImageUrl" alt="大图">
    </el-dialog>
  </div>
</template>

<script>
import service from '@/utils/request';

export default {
  name: 'ArtistDetailVue',
  data() {
    return {
      loading: true,
      artistId: null,
      artist: null,
      activeTab: 'features',
      relatedArtists: [],
      
      // 图片查看对话框
      dialogVisible: false,
      dialogImageUrl: '',
      dialogImageTitle: '',
      
      // 行当数据
      categories: [],
      categoryMap: {},
      
    }
  },
  mounted() {
    this.artistId = this.$route.params.id;
    document.title = '京剧苑 - 艺术家详情';
    window.scrollTo(0, 0);
    // 获取行当数据
    this.fetchCategories().then(() => {
      this.fetchArtistDetail();
    });
  },
  methods: {
    fetchCategories() {
      // 从后端获取行当数据
      return new Promise((resolve) => {
        service.get('/api/content/category/getCategoryList', {
          params: {
            pageNum: 1,
            pageSize: 100
          }
        }).then(res => {
          if (res.code === 200) {
            this.categories = res.data.list;
            // 构建行当ID到名称的映射
            this.categories.forEach(cat => {
              this.categoryMap[cat.categoriesId] = cat.categoriesName;
            });
          } else {
            this.$message.error('获取行当列表失败');
          }
          resolve();
        }).catch(error => {
          this.$message.error('获取行当列表失败：' + (error.message || '未知错误'));
          resolve();
        });
      });
    },
    fetchArtistDetail() {
      this.loading = true;
      
      // 调用后端API获取艺术家详情（已包含所有相关数据）
      service.get(`/api/content/artist/getArtistDetail/${this.artistId}`)
        .then(res => {
          if (res.code === 200 && res.data) {
            // 设置艺术家基本信息
            this.artist = res.data;
            
            // 处理艺术特点信息
            if (this.artist.features && this.artist.features.length > 0) {
              // 转换为展示所需的格式
              this.artist.performanceFeatures = this.artist.features.map(feature => {
                let icon = 'el-icon-star-on';
                
                // 根据特点类型设置不同图标
                if (feature.title && (feature.title.includes('唱') || feature.title.includes('声'))) {
                  icon = 'el-icon-microphone';
                } else if (feature.title && (feature.title.includes('做') || feature.title.includes('动作'))) {
                  icon = 'el-icon-position';
                } else if (feature.title && (feature.title.includes('念') || feature.title.includes('台词'))) {
                  icon = 'el-icon-chat-line-round';
                } else if (feature.title && (feature.title.includes('表') || feature.title.includes('演'))) {
                  icon = 'el-icon-view';
                }
                
                return {
                  title: feature.title || '未命名特点',
                  icon: icon,
                  description: feature.description || '暂无详细描述'
                };
              });
            } else {
              this.artist.performanceFeatures = [];
            }
            
            // 处理作品信息
            if (this.artist.works && this.artist.works.length > 0) {
              // 转换为展示所需的格式
              this.artist.works = this.artist.works.map(work => {
                return {
                  title: work.workTitle,
                  role: work.roleName,
                  year: work.workYear || '不详',
                  image: work.workUrl,
                  description: work.description || '暂无作品描述'
                };
              });
            } else {
              this.artist.works = [];
            }
            
            // 处理历史图片
            if (this.artist.histories && this.artist.histories.length > 0) {
              // 转换为展示所需的格式
              this.artist.photos = this.artist.histories.map(history => {
                return {
                  url: history.url,
                  caption: history.description || '历史图片'
                };
              });
            } else {
              this.artist.photos = [];
            }
          } else {
            this.setDefaultArtistData();
            this.$message.warning('获取艺术家详情失败，将显示默认数据');
          }
        })
        .catch(error => {
          console.error('获取艺术家详情失败:', error);
          this.setDefaultArtistData();
          this.$message.error('获取艺术家详情失败: ' + (error.message || '未知错误'));
        })
        .finally(() => {
          this.loading = false;
        });
    },
    
    setDefaultArtistData() {
      // 设置默认数据
      this.artist = {
        artistId: this.artistId,
        artistName: '未知艺术家',
        category: '未知行当',
        school: '未知流派',
        avatar: require('@/assets/opera_logo.png'),
        description: '暂无详细信息',
        representative: '暂无代表作品',
        artisticFeatures: '暂无艺术特点描述',
        performanceFeatures: [],
        works: [],
        photos: []
      };
    },
    
    
    viewArtistDetail(artistId) {
      if (artistId === this.artistId) return;
      
      // 添加加载状态，提升用户体验
      const loading = this.$loading({
        lock: true,
        text: '正在加载艺术家详情...',
        spinner: 'el-icon-loading',
        background: 'rgba(0, 0, 0, 0.7)'
      });
      
      // 延迟50ms后跳转，让loading效果显示出来
      setTimeout(() => {
        loading.close();
        this.$router.push({ name: 'ArtistDetail', params: { id: artistId }});
      }, 50);
    },
    
    viewLargeImage(url, title) {
      this.dialogImageUrl = url;
      this.dialogImageTitle = title || '';
      this.dialogVisible = true;
    },
    
    getCategoryName(catId) {
      // 从行当映射中获取名称
      return this.categoryMap[catId] || '未知行当';
    },
     // 返回上一页
     goBack() {
      this.$router.push('/artists');
    }
  }
}
</script>

<style scoped>
.artist-detail-container {
  min-height: 100vh;
  padding: 30px 15px 60px;
  max-width: 1200px;
  margin: 0 auto;
  background-color: #fafafa;
}

.back-button {
  margin-bottom: 25px;
  margin-left: 15px;
}

.artist-header {
  background-color: #f8f8f8;
  padding: 40px 5%;
}

.artist-info {
  display: flex;
  max-width: 1200px;
  margin: 0 auto;
}

.artist-avatar {
  width: 200px;
  height: 250px;
  border-radius: 5px;
  overflow: hidden;
  margin-right: 40px;
}

.artist-avatar img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.artist-basic-info {
  flex: 1;
}

.artist-basic-info h1 {
  font-size: 2.5rem;
  color: #333;
  margin: 0 0 15px;
  font-family: 'STZhongsong', serif;
}

.artist-tags {
  margin-bottom: 20px;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 10px;
}

.artist-tags .el-tag {
  margin-right: 0;
  padding: 6px 12px;
  font-size: 14px;
  border-radius: 4px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 60px;
  text-align: center;
}

.category-tag {
  height: 28px;
  line-height: 28px;
}

.artist-bio {
  font-size: 1.1rem;
  line-height: 1.7;
  color: #555;
  margin-bottom: 20px;
}

.artist-meta {
  display: flex;
  flex-wrap: wrap;
}

.meta-item {
  margin-right: 30px;
  margin-bottom: 10px;
  color: #666;
}

.meta-item i {
  margin-right: 5px;
  color: #8B0000;
}

.content-tabs {
  max-width: 1200px;
  margin: 30px auto;
  padding: 0 5%;
}

.tab-content {
  padding: 30px 0;
}

.features-content h2,
.school-content h2,
.related-artists h2 {
  font-size: 1.8rem;
  color: #8B0000;
  margin-bottom: 20px;
  font-family: 'STZhongsong', serif;
}

.feature-cards {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 30px;
  margin-top: 30px;
}

.feature-card {
  display: flex;
  background-color: #f9f9f9;
  border-radius: 8px;
  padding: 20px;
  box-shadow: 0 3px 10px rgba(0, 0, 0, 0.05);
}

.feature-icon {
  font-size: 2.5rem;
  color: #8B0000;
  margin-right: 20px;
  display: flex;
  align-items: center;
}

.feature-text h3 {
  font-size: 1.3rem;
  color: #444;
  margin: 0 0 10px;
}

.feature-text p {
  color: #666;
  line-height: 1.6;
}

.school-info {
  margin-bottom: 40px;
}

.school-info p {
  font-size: 1.1rem;
  line-height: 1.7;
  color: #555;
  margin-bottom: 20px;
}

.school-info h3 {
  font-size: 1.4rem;
  color: #333;
  margin: 30px 0 15px;
}

.school-features {
  list-style: none;
  padding: 0;
}

.school-features li {
  margin-bottom: 15px;
  background-color: #f9f9f9;
  padding: 15px;
  border-radius: 5px;
  border-left: 3px solid #8B0000;
}

.feature-name {
  font-weight: bold;
  color: #8B0000;
}

.feature-desc {
  color: #555;
}

.inheritance-chart {
  margin-top: 50px;
}

.chart-container {
  display: flex;
  flex-direction: column;
  align-items: center;
}

.generation {
  display: flex;
  justify-content: center;
  flex-wrap: wrap;
  width: 100%;
  margin-bottom: 20px;
}

.generation-arrow {
  font-size: 2rem;
  color: #8B0000;
  margin: 10px 0;
}

.artist-node {
  width: 200px;
  margin: 0 20px 20px;
  display: flex;
  flex-direction: column;
  align-items: center;
}

.node-avatar {
  width: 100px;
  height: 100px;
  border-radius: 50%;
  overflow: hidden;
  border: 3px solid #8B0000;
  margin-bottom: 10px;
}

.founder-node .node-avatar {
  width: 120px;
  height: 120px;
  border: 4px solid #FFD700;
}

.node-avatar img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.node-info {
  text-align: center;
}

.node-name {
  font-weight: bold;
  font-size: 1.2rem;
  color: #333;
  margin-bottom: 5px;
}

.node-title {
  font-size: 0.9rem;
  color: #8B0000;
}

.works-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 30px;
}

.work-card {
  display: flex;
  height: 100%;
}

.work-image {
  width: 400px;
  height: 250px;
  overflow: hidden;
  margin-right: 20px;
}

.work-image img {
  width: 100%;
  height: 100%;
  object-fit: contain;
  background: #f5f5f5;
}

.work-info {
  flex: 1;
}

.work-info h3 {
  font-size: 1.4rem;
  color: #333;
  margin: 0 0 10px;
}

.work-info p {
  color: #666;
  line-height: 1.6;
  margin-bottom: 15px;
}

.work-meta {
  display: flex;
  justify-content: space-between;
  color: #8B0000;
  font-size: 0.95rem;
}

.gallery-item {
  margin-bottom: 20px;
}

.photo-card {
  position: relative;
  border-radius: 5px;
  overflow: hidden;
  cursor: pointer;
  box-shadow: 0 3px 10px rgba(0, 0, 0, 0.1);
  transition: all 0.3s ease;
}

.photo-card:hover {
  transform: translateY(-5px);
  box-shadow: 0 10px 20px rgba(0, 0, 0, 0.15);
}

.photo-card img {
  width: 100%;
  height: 200px;
  object-fit: cover;
}

.photo-caption {
  position: absolute;
  bottom: 0;
  left: 0;
  right: 0;
  background: rgba(0, 0, 0, 0.7);
  color: #fff;
  padding: 10px;
  font-size: 0.9rem;
}

.related-artists {
  max-width: 1200px;
  margin: 50px auto 0;
  padding: 0 5%;
}

.related-artists-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 20px;
}

.related-artist-card {
  display: flex;
  align-items: center;
  padding: 15px;
  border-radius: 5px;
  background-color: #f8f8f8;
  cursor: pointer;
  transition: all 0.3s ease;
}

.related-artist-card:hover {
  background-color: #f0f0f0;
  transform: translateY(-3px);
  box-shadow: 0 5px 15px rgba(0, 0, 0, 0.1);
}

.related-avatar {
  width: 50px;
  height: 50px;
  border-radius: 50%;
  overflow: hidden;
  margin-right: 15px;
}

.related-avatar img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.related-info {
  flex: 1;
}

.related-name {
  font-weight: bold;
  color: #333;
  margin-bottom: 5px;
}

.related-category {
  font-size: 0.85rem;
  color: #666;
}

.no-school-data,
.no-works-data,
.no-gallery-data {
  text-align: center;
  padding: 50px 0;
  color: #999;
}

.no-school-data img,
.no-works-data img,
.no-gallery-data img {
  width: 120px;
  margin-bottom: 20px;
  opacity: 0.5;
}

@media (max-width: 992px) {
  .artist-info {
    flex-direction: column;
  }
  
  .artist-avatar {
    margin: 0 auto 20px;
  }
  
  .artist-basic-info {
    text-align: center;
  }
  
  .artist-tags {
    justify-content: center;
  }
  
  .feature-cards {
    grid-template-columns: 1fr;
  }
  
  .works-grid {
    grid-template-columns: 1fr;
  }
  
  .related-artists-grid {
    grid-template-columns: repeat(2, 1fr);
  }
}

@media (max-width: 768px) {
  .artist-basic-info h1 {
    font-size: 2rem;
  }
  
  .related-artists-grid {
    grid-template-columns: 1fr;
  }
  
  .work-card {
    flex-direction: column;
  }
  
  .work-image {
    width: 100%;
    height: 200px;
    margin-right: 0;
    margin-bottom: 15px;
  }
}
</style> 